package GUI;

import game.Battle;
import game.Character;
import game.GameBase;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class BattleView extends JPanel implements ComponentListener,ActionListener,ListSelectionListener
{
	final int menuWidth = 200, menuHeight = 100;
	
	BufferedImage background;
	BufferedImage cursor,healthBar,healthBarCover;
	
	Battle battle;
	ArrayList<game.Character> friendlyCharacters;
	ArrayList<game.Character> enemyCharacters;
	game.Character player;
	
	ButtonSkin skin;
	SkinnedButton attackButton ,castButton,useButton,inventoryButton,spellBookButton;
	JPanel menu1, menu2, menu3;
	ThumbList enemyList;
	int selectedEnemyIndex = 0;
	HUD hud;
	
	public BattleView(Battle battle)
	{
		super();
		
		this.battle = battle;
		healthBarCover = GameBase.getImageByTag("bar-cover");
		healthBar = GameBase.getImageByTag("moves-bar");

		cursor = ImageStorage.getImage("GUI/BattleCursor.png");
		
		hud = new HUD();
		
		this.enemyCharacters = battle.getEnemies();
		
		player = Game.get().getPlayer();

		enemyList = new ThumbList(new ThumbListAdapter() {
			
			@Override
			public String getTooltip(Object o) {
				return ((Character)o).toString();
			}
			
			@Override
			public BufferedImage getThumbnail(Object o) {
				return null;
			}
			
			@Override
			public String getText(Object o) {
				return ((Character)o).getNameHP();
			}

			@Override
			public Color getColor(Object o) {
				return Color.white;
			}
		});
		enemyList.setItemHeight(20);
		
		for(game.Character c :enemyCharacters) 
			enemyList.addElement(c);
		
		
		this.addComponentListener(this);
		this.setLayout(null);
		
		skin = ButtonSkin.getButtonByTag("Button1");
		attackButton = new SkinnedButton(skin,"Attack");
		castButton = new SkinnedButton(skin, "Cast spell");
		useButton = new SkinnedButton(skin,"Use item");
		inventoryButton = new SkinnedButton(skin,"Inventory");
		spellBookButton = new SkinnedButton(skin,"Spell Book");
		
		attackButton.addActionListener(this);
		castButton.addActionListener(this);
		useButton.addActionListener(this);
		inventoryButton.addActionListener(this);
		spellBookButton.addActionListener(this);
		
		this.add(enemyList);
		enemyList.setSize(200,100);
		enemyList.setBackground(Color.gray);
//		enemyList.setOpaque(false);
		enemyList.addListSelectionListener(this);
		enemyList.setSelectedIndex(0);
		
		menu1 = new JPanel();
		menu2 = new JPanel();
		menu3 = new JPanel();
		
		menu1.setLayout(new BoxLayout(menu1, BoxLayout.Y_AXIS));
		menu2.setLayout(new BoxLayout(menu2, BoxLayout.Y_AXIS));
//		menu3.setLayout(new BoxLayout(menu3, BoxLayout.Y_AXIS));
		
		menu1.add(attackButton);
		menu1.add(castButton);
		menu1.add(useButton);
		menu2.add(inventoryButton);
		menu2.add(spellBookButton);
//		menu3.add(enemyList);
		
		this.add(menu1);
		menu1.setSize(menuWidth,menuHeight);
		menu1.setVisible(true);
		
		this.add(menu2);
		menu2.setSize(menuWidth,menuHeight);
		menu2.setVisible(true);
		
//		this.add(menu3);
		menu3.setSize(menuWidth,menuHeight);
		menu3.setBackground(Color.gray);
		CalculateCoordinates();
	}
	
	public void setBackground(BufferedImage image)
	{
		background = image;
	}
	
	private void CalculateCoordinates()
	{
		int nesto = this.getWidth()-100;
		
		int y = ((this.getHeight() - menuHeight) - enemyCharacters.size()*64)/2;
		int x = nesto - enemyCharacters.size() * 32;
		int i = 0;
				
		for(game.Character c:enemyCharacters)
		{
			c.battleX = x;
			c.battleY = y;
			
			i++;
			y+= 64;
			x-=32;
		}
		
		player.battleX = 50; 
		player.battleY = (this.getHeight() - menuHeight )/2;
	}
	
	public void paintComponent(Graphics gr)
	{
		Graphics2D g = (Graphics2D)gr;
		
		g.drawImage(background, 0, 0, this.getWidth(), this.getHeight() , null);
		
		hud.drawBarWH(g, healthBar, healthBarCover, menuWidth*2, this.getHeight()-menuHeight, this.getWidth() - menuWidth*3, menuHeight/2, player.getMaxHealth(), player.getHealth());
		hud.drawBarWH(g, healthBar, healthBarCover, menuWidth*2, this.getHeight()-menuHeight/2, this.getWidth() - menuWidth*3, menuHeight/2, player.getMaxMana(), player.getMana());
		
		String healthText = "Health: " + player.getHealth() + "/" + player.getMaxHealth();
		String manaText = "Mana: " + player.getMana() + "/" + player.getMaxMana();
		
		ButtonSkin.drawText(g, healthText, Color.white, menuWidth*2, this.getHeight()-menuHeight, this.getWidth()-menuWidth*3, menuHeight/2, ButtonSkin.CENTER, true);
		ButtonSkin.drawText(g, manaText, Color.white, menuWidth*2, this.getHeight()-menuHeight/2, this.getWidth()-menuWidth*3, menuHeight/2, ButtonSkin.CENTER, true);
		
	
		int i = 0;
		
//		DanceToCenter(player, null, g);
		
		for(game.Character c:enemyCharacters)
		{
			BufferedImage image = c.sikira.idle;
			g.drawImage(image, c.battleX, c.battleY, 64, 64, null);
			
			if(i == selectedEnemyIndex)
			{
				g.drawImage(cursor, c.battleX-32, c.battleY, 16, 16, null);
			}
			
			i++;
		}
		
		g.drawImage(player.sikira.idle, player.battleX, player.battleY, 64, 64, null);
		
		
	}

	private void DanceToCenter(final Character c1,final Character c2) 
	{
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				int centerX = getWidth() / 2;
				int centerY = (getHeight() - menuHeight) / 2;
				
				int startX1 = 50,startY1 = centerY;
				
				int nesto = getWidth()-100;
				
				/*int startX2 = (nesto - enemyCharacters.size() * 32) - enemyCharacters.size() * 32;
				int startY2 = (((getHeight() - menuHeight) - enemyCharacters.size()*64)/2) + enemyCharacters.size() * 64;*/
				
				int startX2 = c2.battleX;
				int startY2 = c2.battleY;
				
				for(int x1 = startX1,x2 = startX2;x1 <= centerX && x2 > centerX;x1+=10,x2-=10)
				{
					int y1 = startY1 + (x1 - startX1)*(centerY - startY1)/(centerX-startX1);
					int y2 = startY2 + (x2 - startX2)*(centerY - startY2)/(centerX-startX2);
					
					/*if(x2 <= centerX) x2 = centerX;
					if(x1 >= centerX) x1 = centerX;*/
					
					c1.battleX = x1;
					c1.battleY = y1;
					
					c2.battleX = x2;
					c2.battleY = y2;
					
					repaint();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}				
			}
		});
		t.start();
	}
	
	public void componentResized(ComponentEvent e) 
	{
		menu1.setLocation(0,this.getHeight()-menu1.getHeight());
		menu2.setLocation(menuWidth,this.getHeight()-menu2.getHeight());
		enemyList.setLocation(this.getWidth() - menuWidth,this.getHeight()-menuHeight);
//		menu3.setLocation(this.getWidth() - menuWidth,this.getHeight()-menuHeight);
		CalculateCoordinates();
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentShown(ComponentEvent e) 
	{
		menu1.setLocation(0,this.getHeight()-menu1.getHeight());
		
	}

	
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	
	public void actionPerformed(ActionEvent arg0) 
	{
		if(arg0.getActionCommand() == "Attack")
		{
			if(battle.getNaPotezu() == player)
			{
				battle.napadni(enemyCharacters.get(enemyList.getSelectedIndex()));
				battle.prebaciPotez();
				
				game.Character c = enemyCharacters.get(enemyList.getSelectedIndex());
				
				DanceToCenter(player, c);
				enemyList.repaint();
			}
			else {
				battle.napadni(player);
				battle.prebaciPotez();
			}
		}
		
		repaint();
	}

	
	public void valueChanged(ListSelectionEvent e) 
	{
		selectedEnemyIndex = enemyList.getSelectedIndex();
		repaint();
	}
}
